#include "message.h"
#include "FTP_server.h"
#include "pack.h"
#include "unpack.h"

int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        printf("%s <IP> <PORT>\n",argv[0]);
        return -1;
    }
    //1.创建套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        perror("socket failed");
        return -1;
    }
    //设置IP地址和端口重用设置
    int on = 1;
    socklen_t len = sizeof(int);
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR|SO_REUSEPORT,(void*)&on,len);
    //网络地址的设置
    struct sockaddr_in serv;
    serv.sin_family = AF_INET;
    serv.sin_port = htons(atoi(argv[2]));
    serv.sin_addr.s_addr = inet_addr(argv[1]);
    //绑定
    int ret = bind(sockfd,(struct sockaddr*)&serv,sizeof(serv));
    if(ret == -1)
    {
        perror("bind failed");
        close(sockfd);
        return -1;
    }
    //进入监听模式
    ret = listen(sockfd,10);
    if(ret == -1)
    {
        perror("listen failed");
        close(sockfd);
        return -1;
    }
    while(1)
    {
        unsigned mima[4000]={0};//接受密码
        //每当有一个客户端连接成功之后，开辟一个子进程去与其通信，父进程依旧等待新的客户端
        struct sockaddr_in client;//用来保存客户端的地址 
        socklen_t len = sizeof(client);
        int confd = accept(sockfd,(struct sockaddr *)&client,&len);
        if(confd == -1)
        {
            perror("accept failed");
            continue;
        }
        printf("connect %s[port:%d] success!!\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));//打印客户端的信息 
        pid_t pid = fork();
        if(pid > 0)//父进程
        {
            close(confd);
            continue;
        }
        else if(pid == 0)//子进程负责通信
        {
            while(1)
            {
                
                unsigned char recv_cmd[1024] = {0};//接收客户端封装的数据包数组
                unsigned char resp[10240] = {0};//回复数据包数组
                enum CMD_NO cmd;//命令号
                int arg_len = 0;//参数长度 
                char arg[128] = {0};//参数字符串
                ret = recv(confd,recv_cmd,1023,0);//接收客户端发送来的数据包
                int cd_cmd = recv_cmd[5]|(recv_cmd[6]<<8)|(recv_cmd[7]<<16)|(recv_cmd[8]<<24);
                //printf("接收到的cd_cmd:%d\n",cd_cmd);
                hand_cmd(recv_cmd,&cmd,&arg_len,arg);//参数
                printf("获取到的命令号：%d\n",cmd);
                if(cmd == FTP_CMD_LS)//命令是ls
                {
                    ser_hand_ls(confd,resp,cmd);
                }
                else if(cmd == FTP_CMD_GET)//命令是get
                {
                    ser_hand_get(confd,resp,cmd,arg);
                }
                else if(cmd == FTP_CMD_PUT)//命令是put
                {
                    ser_hand_put(confd,resp,cmd,arg);
                }
                else if(cmd == FTP_CMD_CD)//命令是cd
                {
                    //printf("进入了cd命令的处理函数\n");
                    char nserdir[256];
                    ser_hand_cd(arg,nserdir,confd);
                    printf("Serdir:%s\n",Serdir);
                }
                else if(cmd == FTP_CMD_BYE)
                {
                    ser_hand_bye(confd,resp,cmd,client);
                    exit(0);
                }
                else if(cmd == FTP_CMD_UNKNOWN)
                {
                    ser_hand_unknown(confd,resp,cmd);
                }
                else if(cmd== FTP_CMD_PWD)
                {
                    ser_hand_pwd(confd,resp,cmd);
                }
            }
        }   
    }
    close(sockfd);
    return 0;
}